/* Copyright 2012 Dustin Mitchell <dmmitche [at] gmail [dot] com>
 *
 * Licensed under the GPLv3.  See included LICENSE.txt for full details.
 */

#ifndef __REGISTERS__
#define __REGISTERS__

#include <sys/types.h>
#include <stdint.h>

#define SIZE_1K 0x400
#define SIZE_2K 0x800
#define SIZE_4K 0x1000
#define SIZE_8K 0x2000
#define SIZE_16K 0x4000
#define SIZE_32K 0x8000
#define SIZE_64K 0x10000
#define SIZE_128K 0x20000

struct reg_map
{
	int dev_mem_fd;
	void *mem_mapping;
	size_t map_length;
	off_t map_offset;
};

int registers_map (struct reg_map *mapping, long addr, size_t length);

void registers_unmap (struct reg_map *mapping);

void registers_map_print (struct reg_map *mapping);

#define registers_get_reg_addr(m,o,t) \
	(volatile t *)((uint8_t *)(m)->mem_mapping + (o) + (m)->map_offset)

#define REG8(b,o) *(volatile uint8_t *)((volatile uint8_t *)b + o)
#define REG16(b,o) *(volatile uint16_t *)((volatile uint8_t *)b + o)
#define REG32(b,o) *(volatile uint32_t *)((volatile uint8_t *)b + o)

#endif /* _REGISTERS__ */
